<?php
namespace app\login\controller;
use think\Session;
use think\Db; // 引用DB类
use think\Request;
class Login
{

    // http://127.0.0.1:85/index.php?s=/tdled/test
    /*
    {
        "GDH": "AK1970254",
        "LSM": "002"
    }
	*/
    public function index($data)
    {
		$data1 = json_decode($data, true); // 解析成数组对象
		return $data1['register_phone'];
		//return json($data);
        $GDH = Db::table("test")
            ->where("id", 1)
            ->field("name, age")
            ->find();
        
        return json($GDH);
    }
    
    //处理登录
    /*
     * 登录成功 返回code=>200
     * 否色返回 code=>500
     * **/
    public function doLogin(){
    	$login_name 	 = request()->post('login_name');//登录名
	    $login_pass 	 = request()->post('login_pass');//登录密码
	    
	    $login_pass		= sha1(md5($login_pass));
	    
	    $isLogin		= Db::table("login")
	    			   -> where("login_name", $login_name)
	    			   -> where("login_pass", $login_pass)
	    			   -> select();
	    if($isLogin){
			session_start();
	    	//Session::set('login_name',$login_name);//存入session
	    	//Session::set('lately_logintime',date("Y-m-d H:i:s"));
			$_SESSION['login_name']=$login_name;
			$_SESSION['login_pass']=$login_pass;
			/*$role	= "";
			if($isLogin[0]["register_role"]==1){
				$role = "普通用户";
			}else{
				$role = "超级管理员";
			}*/
			//var_dump($isLogin[0]["register_role"]);
	    	return json(["msg"=>"登录成功","code"=>200,"login_role"=>$isLogin[0]["register_role"],"login_name"=>$_SESSION['login_name']]);
	    }else{
	    	return json(["msg"=>"用户名或密码错误","code"=>500]);
	    }
    	
    }
	
	
	public function doLoginfont(){
    	$login_name 	 = request()->post('name');//登录名
	    $login_pass 	 = request()->post('password');//登录密码
	    $login_pass		= sha1(md5($login_pass));
	    $isLogin		= Db::table("login_font")
	    			   -> where("login_name", $login_name)
	    			   -> where("login_pass", $login_pass)
	    			   -> select();
		
	    if($isLogin){
			session_start();
	    	//Session::set('login_name',$login_name);//存入session
	    	//Session::set('lately_logintime',date("Y-m-d H:i:s"));
			$_SESSION['login_name']=$login_name;
			$_SESSION['login_pass']=$login_pass;
			/*$role	= "";
			if($isLogin[0]["register_role"]==1){
				$role = "普通用户";
			}else{
				$role = "超级管理员";
			}*/
			//var_dump($isLogin[0]["register_role"]);
			
			$data = ['user_is_online' => 1];
			Db::table('user_font')
				->where('user_name',$login_name)
				->update($data);
			
			$a	= Db::query("
					SELECT *
FROM
	balance_font
WHERE
	user_name = '".$login_name."'");
			
			
			
	    	return json(["msg"=>"登录成功","data"=>$a,"code"=>200,"login_role"=>$isLogin[0]["register_role"],"login_name"=>$_SESSION['login_name']]);
	    }else{
	    	return json(["msg"=>"用户名或密码错误","code"=>500]);
	    }
    	
    }
	
	
	
	public function fontupdatapassword($name,$password,$yespassword){
    	
	    $password		= sha1(md5($password));
	    $isLogin		= Db::table("login_font")
	    			   -> where("login_name", $name)
	    			   -> where("login_pass", $password)
	    			   -> select();
		$yespassword		= sha1(md5($yespassword));
	    if($isLogin){
			$data = ['login_pass' => $yespassword];
			$data1 = ['register_password' => $yespassword];
			$A=Db::table('login_font')
				->where('login_name',$name)
				->update($data);
			$B=Db::table('register_font')
				->where('register_phone',$name)
				->update($data1);
			return json(["msg"=>"修改成功","code"=>200]);
	    }else{
	    	return json(["msg"=>"用户名或密码错误","code"=>500]);
	    }
    	
    }
	
	
	
	
	
	public function getsessions($data){
		session_start();
		$username=$_SESSION['login_name'];
		if ($username) {
			//if(isset($_SESSION["lately_logintime"])){}
			if ((strtotime(date("Y-m-d H:i:s"))-strtotime($_SESSION["lately_logintime"]))>10){
				return json(["msg"=>"登录超时,请重新登录","code"=>500,"login_name"=>$username]);
			}
			else{
				return json(["msg"=>"登录成功","code"=>200,"login_name"=>$username]);
			}
		}
		else{
			return json(["msg"=>"您未登录,请先去登录","code"=>500,"login_name"=>$username]);
		}
		
    }

    public function read()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) 2018 READ新年快乐</h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }
	
	public function testshamd5()
    {
		
		$str = "中文网";

		echo "MD5的加密结果：".md5($str)."";

		echo "sha1的加密结果：".sha1($str)."";
		
        $GDH = Db::table("test")
            ->where("id", 1)
            ->field("name, age")
            ->find();
        
        return json($GDH);
    }
}
